package com.skwen.android_room;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class AppExecutors {

    private Executor diskIO, networkIO, mainThread;

    private static AppExecutors sAppExecutors;

    private AppExecutors(Executor diskIO,
                         Executor networkIO,
                         Executor mainThread) {
        this.diskIO = diskIO;
        this.networkIO = networkIO;
        this.mainThread = mainThread;
    }

    private static class MainThreadExecutor implements Executor {

        Handler mainThreadHandler = new Handler(Looper.getMainLooper());

        @Override
        public void execute(Runnable runnable) {
            mainThreadHandler.post(runnable);
        }
    }


    public static AppExecutors getInstance() {
        if (sAppExecutors == null) {
            synchronized (AppExecutors.class) {
                if (sAppExecutors == null) {
                    sAppExecutors = new AppExecutors(
                            Executors.newSingleThreadExecutor(), Executors.newFixedThreadPool(3),
                            new MainThreadExecutor()
                    );
                }
            }
        }

        return sAppExecutors;
    }

    public Executor getDiskIO() {
        return diskIO;
    }

    public Executor getNetworkIO() {
        return networkIO;
    }

    public Executor getMainThread() {
        return mainThread;
    }
}
